package com.gmrz.uap.cfg;

import com.gmrz.uap.common.AuthInterceptor;
import com.gmrz.uap.common.ControllerSupport;
import com.gmrz.uap.common.DbConst;
import com.gmrz.uap.common.UAPSSOHelper;
import com.gmrz.uap.model.Resource;
import com.gmrz.uap.model.Tenant;
import com.gmrz.uap.model.User;
import com.gmrz.uap.resource.ResourceService;
import com.gmrz.uap.tenant.TenantService;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.List;


public class SystemController extends ControllerSupport {

    private static final Logger LOG = Logger.getLogger(SystemController.class);

    public void index() {

        User user = UAPSSOHelper.getUserByUapSessionId(getRequest());
        user = User.dao.findById(user.getUserId());

        List<Tenant> tenantList;//如果用户为超级管理员则看到所有的渠道，渠道管理员则可以被分配的渠道
        if (StringUtils.isBlank(user.getTenantId())) {
            tenantList = TenantService.me.getAllTenants();
        } else {
            tenantList = TenantService.me.getAllTenants(user.getTenantId());
        }
        setAttr("tenants", tenantList);

       /* if (StrKit.notBlank(user.getTenantId())) {//如果登录用户的渠道ID不为空，则将COOKIE中的渠道ID设置为当前用户渠道ID
            int maxAgeInSeconds = 30 * 24 * 60 * 60;
            String tenantId;
            if (user.getTenantId().contains(SystemConst._DEFAULT_SEPARATOR_COMMA)) {
                tenantId = user.getTenantId().split(SystemConst._DEFAULT_SEPARATOR_COMMA)[0];
            } else {
                tenantId = user.getTenantId();
            }
            setCookie(SystemConst.COOKIE_TENANT_ID, tenantId, maxAgeInSeconds, "/");
        }*/

        //从COOKIE中获取TenantID，查询得到当前租户信息
        String tenantId = getCookie("tenantId");
        if ((StringUtils.isNotBlank(user.getTenantId()) && !user.getTenantId().contains(tenantId)) || StringUtils.isBlank(tenantId)) {
            tenantId = tenantList.get(0).getTenantId();
            setAttr("currTenant", tenantList.get(0));
            setAttr("currTenantId", tenantId);

            int maxAgeInSeconds = 30 * 24 * 60 * 60;
            setCookie(SystemConst.COOKIE_TENANT_ID, tenantId, maxAgeInSeconds, "/");
        } else {
            Tenant currTenant = TenantService.me.getByTenantId(tenantId);
            setAttr("currTenant", currTenant);
            setAttr("currTenantId", tenantId);
        }

        //获取当前用户相关的菜单
        List<Resource> resourceList = ResourceService.me.findByUserIdAndPid(user.getStr(DbConst.TABLE_USER_COLUMN_USER_ID), "0");
        if (!resourceList.isEmpty()) {
            for (int i = 0; i < resourceList.size(); i++) {
                Resource resource = resourceList.get(i);
                List<Resource> resourceTmpList = ResourceService.me.findByUserIdAndPid(user.getStr(DbConst.TABLE_USER_COLUMN_USER_ID), resource.getResourceId());
                resource.put("menuList", resourceTmpList);
                resourceList.set(i, resource);
            }
        }

        //获取系统版本
        Prop manifestProp = PropKit.use(new File(SystemConst.getWebRootUrl(this.getSession()) + "/META-INF/MANIFEST.MF"));
        setAttr("systemVersion", manifestProp.get("Uap-Version"));

        setAttr("resourceList", resourceList);

    }

    public void main() {

    }


    @Clear(AuthInterceptor.class)
    public void captcha() {
        renderCaptcha();
    }
}
